package com.dirox.plugins.issuetab;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.jql.builder.JqlClauseBuilder;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.plugin.issuetabpanel.AbstractIssueAction;
import com.atlassian.jira.plugin.issuetabpanel.IssueTabPanelModuleDescriptor;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.user.util.UserManager;
import com.dirox.plugins.ao.entities.Note;
import com.dirox.plugins.ao.service.NoteService;

public class NoteIssueAction extends AbstractIssueAction {
	private final JiraAuthenticationContext context;
	
	private IssueService issueService;
	private SearchService searchService;
	private ProjectManager projectManager;
	private  NoteService noteService;
	private  UserManager userManager;
	private Issue currentIssue;
	public NoteIssueAction(IssueTabPanelModuleDescriptor descriptor,
			JiraAuthenticationContext context, IssueService issueService,
			SearchService searchService, ProjectManager projectManager,
			NoteService noteService, UserManager userManager) {
		super(descriptor);
		this.context = context;
		this.noteService = checkNotNull(noteService);
		this.userManager = checkNotNull(userManager);
		this.searchService = checkNotNull(searchService);
		this.issueService = checkNotNull(issueService);
		this.projectManager = checkNotNull(projectManager);
	}

	public List<Issue> getIssues(String projectName) {
		final User currentUser = context.getLoggedInUser();
		JqlClauseBuilder jqlClauseBuilder = JqlQueryBuilder.newClauseBuilder();
		com.atlassian.query.Query query = jqlClauseBuilder.project(projectName)
				.buildQuery();

		PagerFilter pagerFilter = PagerFilter.getUnlimitedFilter();
		com.atlassian.jira.issue.search.SearchResults searchResults = null;
		try {
			searchResults = searchService.search(currentUser, query,
					pagerFilter);
		} catch (SearchException e) {
			e.printStackTrace();
		}
		return searchResults.getIssues();
	}
	
	public List<Issue> getIssues() {
		List<Issue> issues = new ArrayList<Issue>();
		List<Project> projects = projectManager.getProjectObjects();
		for (Project p : projects) {
			issues.addAll(getIssues(p.getName()));
		}
		return issues;
	}
	@Override
	public Date getTimePerformed() {
		return new Date();
	}

	@Override
	protected void populateVelocityParams(Map params) {
		final User currentUser = context.getLoggedInUser();
		List<Note> notes = noteService.getNotes(currentIssue.getKey());
		List<Issue> issues =  getIssues();
		
		List<User> users = new ArrayList<User>();
		users.addAll(userManager.getAllUsers());
		users.remove(currentUser);
		List<Note> memos = noteService.getMemos(currentIssue.getKey());
		
		if (notes.size() > 0 || memos.size() > 0) {
			params.put("hasLastNote", 1);
		}
		else {
			params.put("hasLastNote", 0); 
		}

		params.put("memos", memos);
		params.put("issues", issues);
		params.put("notes", notes);
		params.put("users", users);
		params.put("currentIssue", this.currentIssue);
	}
	
	//---------------------------------------------------------------------------------------
			public Issue getIssue() {
				return this.currentIssue;
			}

			public void setIssue(Issue i) {
				this.currentIssue = i;
			}
}
